﻿
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Entity;
using BAL;
using Common;
using System.Data.OleDb;
using System.IO;
namespace Web
{
    public partial class PostAllUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //批量导入用户的功能页面
        protected void btnAddAll_Click(object sender, EventArgs e)
        {

            if (FileUpload1.PostedFile.FileName == "")
            {
                Response.Write("<script>alert('要上传的文件不允许为空');</script>");
            }
            else
            {
                //记录身份证已经存在的人的人名
                String nameofExitIdCard = "";
                //记录Email已存在的人名
                String nameofExitEmail = "";
                //记录学校不匹配的人名
                String nameOfExitSchool = "";
                try
                {   
                    string filepath = FileUpload1.PostedFile.FileName;
                    string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
                    //对文件的格式进行判断
                    if(filename.Substring(filename.LastIndexOf('.')+1,3)!="xls")
                    {
                        Response.Write("<script>alert('您上传的文件的格式有误，请检查后重新上传');</script>");
                        return;
                    }
                    string serverpath = Server.MapPath("FileExcel/") + filename;
                    FileUpload1.PostedFile.SaveAs(serverpath);
                    //文件上传成功之后
                    string strCon;
                    strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("FileExcel/教师用户信息.xls") + "; Extended Properties=Excel 8.0;";
                    OleDbConnection olecon = new OleDbConnection(strCon);
                    OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
                    DataSet myds = new DataSet();
                    myda.Fill(myds);
                    //对数据的处理
                    for (int i = 0; i < myds.Tables[0].Rows.Count; i++)
                    {
                        //string updateSql1="insert into tantest(ghid,xjhao,ylao,yliao,gjj)values('"+myds.Tables[0].Rows[i]["工号"]+"','"+myds.Tables[0].Rows[i]["薪金号"]+"','"+myds.Tables[0].Rows[i]["养老基金"]+"','"+myds.Tables[0].Rows[i]["医疗基金"]+"','"+myds.Tables[0].Rows[i]["公积金"]+"')"; 
                        if (myds.Tables[0].Rows[i]["身份证号"].ToString() == null || myds.Tables[0].Rows[i]["身份证号"].ToString()=="")
                        {
                            continue;
                        }
                        //首先验证身份证查看此人是否已经存在
                        try
                        {
                            if (!UserExtend.CheckUserExistIdCard(myds.Tables[0].Rows[i]["身份证号"].ToString()))
                            {
                                if (!Users.CheckUserExistByEmail(myds.Tables[0].Rows[i]["E-Mail"].ToString()))
                                {

                                    UserExtendInfo ueinfo = new UserExtendInfo();
                                    //对学校的处理
                                    if (!(myds.Tables[0].Rows[i]["任教学校"].ToString() == null || myds.Tables[0].Rows[i]["任教学校"].ToString() == null))
                                    {
                                        string aa = myds.Tables[0].Rows[i]["任教学校"].ToString();
                                        SchoolInfo ss = School.GetSingleInfoBySchoolName(aa);
                                        if (ss != null)
                                        {
                                            ueinfo.SchoolId = ss.Id;
                                            ueinfo.School = myds.Tables[0].Rows[i]["任教学校"].ToString();
                                        }
                                        else
                                        {
                                            nameOfExitSchool += myds.Tables[0].Rows[i][""].ToString();
                                            continue;
                                        }

                                    }
                                    UserInfo userInfo = new UserInfo();
                                    userInfo.Name = myds.Tables[0].Rows[i]["姓名"].ToString();
                                    userInfo.PinYin = Module_String.GetPYString(myds.Tables[0].Rows[i]["姓名"].ToString());
                                    userInfo.PassWord = Utility.MD5(myds.Tables[0].Rows[i]["密码"].ToString());
                                    userInfo.RoleId = (Role.GetRoleInfoByRoleName(myds.Tables[0].Rows[i]["权限"].ToString())).Id;
                                    string statusOfTeacher = myds.Tables[0].Rows[i]["状态"].ToString();
                                    if (statusOfTeacher == "已审核")
                                    {
                                        userInfo.Status = 2;
                                    }
                                    else if (statusOfTeacher == "未激活")
                                    {
                                        userInfo.Status = 3;
                                    }
                                    else
                                    {
                                        userInfo.Status = 1;//未审核状态
                                    }

                                    userInfo.Email = myds.Tables[0].Rows[i]["E-Mail"].ToString();
                                    userInfo = Users.Add(userInfo);

                                    ueinfo.UserId = userInfo.Id;
                                    if (!(myds.Tables[0].Rows[i]["家庭住址"].ToString() == "" || myds.Tables[0].Rows[i]["家庭住址"] == null))
                                    {
                                        ueinfo.Address = myds.Tables[0].Rows[i]["家庭住址"].ToString();
                                    }
                                    if (!(myds.Tables[0].Rows[i]["年龄"] == null || myds.Tables[0].Rows[i]["年龄"].ToString() == ""))
                                    {
                                        ueinfo.Age = byte.Parse(myds.Tables[0].Rows[i]["年龄"].ToString());
                                    }
                                    if (myds.Tables[0].Rows[i]["职称"] == null || myds.Tables[0].Rows[i]["职称"].ToString() == "")
                                    {
                                        ueinfo.Caste = Convert.ToByte(1);
                                    }
                                    else
                                    {
                                        ueinfo.Caste = Convert.ToByte(myds.Tables[0].Rows[i]["职称"]);
                                    }
                                    if (myds.Tables[0].Rows[i]["职务"] == null || myds.Tables[0].Rows[i]["职务"].ToString() == "")
                                    {
                                        ueinfo.Business = Convert.ToByte(1);
                                    }
                                    else
                                    {
                                        ueinfo.Business = Convert.ToByte(myds.Tables[0].Rows[i]["职务"]);
                                    }
                                    ueinfo.Intro = myds.Tables[0].Rows[i]["个人简介"].ToString();
                                    ueinfo.Postcode = myds.Tables[0].Rows[i]["邮政编码"].ToString();


                                    if (!(myds.Tables[0].Rows[i]["性别"].ToString() == "" || myds.Tables[0].Rows[i]["性别"] == null))
                                    {
                                        ueinfo.Sex = Convert.ToByte(myds.Tables[0].Rows[i]["性别"].ToString());
                                    }
                                    ueinfo.Tel = myds.Tables[0].Rows[i]["联系电话"].ToString();
                                    ueinfo.IdCard = myds.Tables[0].Rows[i]["身份证号"].ToString();

                                    UserExtendInfo uu = UserExtend.Save(ueinfo);

                                }
                                else
                                {
                                    nameofExitEmail += myds.Tables[0].Rows[i]["姓名"] + "   ";
                                }

                            }
                            else
                            {
                                nameofExitIdCard += myds.Tables[0].Rows[i]["姓名"] + "    ";
                            }
                        }
                        catch (Exception ex )
                        {
                            continue;
                        }
                        
                        continue;
                            
                    }
                    //记录需要检查身份证号的人的名称
                    if (nameofExitIdCard == "" || string.IsNullOrEmpty(nameofExitIdCard))
                    {
                        Session["nameofIdCard"] = null;
                    }
                    else 
                    {
                        Session["nameofIdCard"] = nameofExitIdCard;
                    }
                    if (nameofExitEmail == "" || string.IsNullOrEmpty(nameofExitEmail))
                    {
                        Session["nameofEmail"] = null;
                    }
                    else 
                    {
                        //记录需要检查Email的人的名称
                        Session["nameofEmail"] = nameofExitEmail;
                    }
                    if (nameOfExitSchool == "" || string.IsNullOrEmpty(nameOfExitSchool))
                    {
                        Session["nameOfExitSchool"] = null;
                    }
                    else 
                    {
                        Session["nameOfExitSchool"] = nameOfExitSchool;
                    }
                    Session["begin"] = 1;
                   
                    //删除文件
                    Delete(serverpath);
                    //刷新页面
                    Utility.ReloadTopPage(this);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
               

            }
        }
        //删除文件
        public static void  Delete(string path)
        {
            try
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
               
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
